package com.imooc.controller;


import com.imooc.base.BaseInfoProperties;
import com.imooc.grace.result.GraceJSONResult;
import com.imooc.pojo.BO.CreateAdminBO;
import com.imooc.pojo.BO.ResetPwdBO;
import com.imooc.service.AdminService;
import com.imooc.utils.PagedGridResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * app端一键注册登录
 */
@RestController
@RequestMapping("admininfo")
@Slf4j
public class AdminController extends BaseInfoProperties {

    @Autowired
    private AdminService adminService;

    @PostMapping("create")
    public GraceJSONResult create(@Valid @RequestBody CreateAdminBO createAdminBO) {
        adminService.createAdmin(createAdminBO);
        return GraceJSONResult.ok();
    }

    @PostMapping("list")
    public GraceJSONResult list(String accountName,
                                Integer page,
                                Integer limit) {

        if (page == null) page = 1;
        if (limit == null) limit = 10;

        PagedGridResult listResult = adminService.getAdminList(accountName, page, limit);

        return GraceJSONResult.ok(listResult);
    }

    @PostMapping("delete")
    public GraceJSONResult delete(String username) {

        adminService.deleteAdmin(username);

        return GraceJSONResult.ok();
    }

    @PostMapping("resetPwd")
    public GraceJSONResult resetPwd(@RequestBody ResetPwdBO resetPwdBO) {

        resetPwdBO.modifyPwd();
        return GraceJSONResult.ok();
    }

}
